# 使用官方 Ubuntu 24.04 镜像
FROM ubuntu:24.04

# 设置非交互式安装
ENV DEBIAN_FRONTEND=noninteractive

# ========================
# 🇨🇳 替换 Ubuntu APT 源为清华镜像
# ========================
RUN sed -i 's|http://.*archive.ubuntu.com|https://mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list && \
    sed -i 's|http://.*security.ubuntu.com|https://mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list

# 安装基础工具和依赖
RUN apt-get update && apt-get install -y \
    curl \
    wget \
    git \
    vim \
    sudo \
    zsh \
    locales \
    ca-certificates \
    build-essential \
    gdb \
    cmake \
    ninja-build \
    clang \
    clang-format \
    clang-tidy \
    lldb \
    valgrind \
    && rm -rf /var/lib/apt/lists/*

# 设置 UTF-8 locale 和中文本地化
RUN apt-get update && apt-get install -y \
    language-pack-zh-hans \
    && rm -rf /var/lib/apt/lists/*

# 生成中英文 locale
RUN locale-gen en_US.UTF-8 zh_CN.UTF-8 zh_CN.GBK

# 设置默认语言环境
ENV LANG=zh_CN.UTF-8 \
    LANGUAGE=zh_CN:zh:en_US:en \
    LC_ALL=zh_CN.UTF-8

# 设置时区为上海
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# ========================
# 🐍 安装 Python 3.12 + pip + 常用包
# ========================
RUN apt-get update && apt-get install -y \
    python3 \
    python3-pip \
    python3-venv \
    python3-full \
    python3-dev \
    build-essential \
    pkg-config \
    && rm -rf /var/lib/apt/lists/*

# 🇨🇳 设置 pip 国内镜像（清华源）
RUN pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple && \
    pip3 config set install.trusted-host pypi.tuna.tsinghua.edu.cn

# 创建虚拟环境并安装常用包（解决 PEP 668 限制）
RUN python3 -m venv /opt/venv && \
    /opt/venv/bin/pip install --upgrade pip && \
    /opt/venv/bin/pip install --no-cache-dir \
    requests \
    numpy \
    matplotlib \
    flask \
    jupyter

# 安装pandas（使用预编译的wheel版本避免构建问题）
RUN /opt/venv/bin/pip install --no-cache-dir pandas --only-binary=all

# 将虚拟环境添加到系统PATH
ENV PATH="/opt/venv/bin:$PATH"

# ========================
# 🌐 安装 Node.js 20.x (LTS) + 设置 npm 镜像
# ========================
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
    apt-get install -y nodejs

# 🇨🇳 设置 npm 淘宝镜像 + yarn/pnpm
RUN npm config set registry https://registry.npmmirror.com && \
    npm install -g yarn pnpm && \
    yarn config set registry https://registry.npmmirror.com

# ========================
# ☕ 安装 Java 21 (OpenJDK) + Maven + Gradle
# ========================
RUN apt-get update && apt-get install -y \
    openjdk-21-jdk \
    maven \
    gradle \
    && rm -rf /var/lib/apt/lists/*

# 🇨🇳 设置 Maven 阿里云镜像
RUN mkdir -p /root/.m2 && \
    printf '<?xml version="1.0" encoding="UTF-8"?>\n<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"\n          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0\n          http://maven.apache.org/xsd/settings-1.0.0.xsd">\n  <mirrors>\n    <mirror>\n      <id>aliyunmaven</id>\n      <mirrorOf>*</mirrorOf>\n      <name>阿里云公共仓库</name>\n      <url>https://maven.aliyun.com/repository/public</url>\n    </mirror>\n  </mirrors>\n</settings>' > /root/.m2/settings.xml

# 创建开发用户 dev
RUN useradd -m dev

# 为 dev 用户也设置 Maven 镜像
RUN mkdir -p /home/dev/.m2 && \
    cp /root/.m2/settings.xml /home/dev/.m2/ && \
    chown -R dev:dev /home/dev/.m2

# ========================
# 🐹 安装 Go 1.22+ + 设置 GOPROXY
# ========================
ENV GO_VERSION=1.22.5
ENV GO_HOME=/usr/local/go
ENV PATH=$PATH:$GO_HOME/bin

# 🇨🇳 设置 Go 代理（七牛/阿里云）
ENV GOPROXY=https://goproxy.cn,direct
# 或使用阿里云：https://mirrors.aliyun.com/goproxy/

RUN wget -q https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz && \
    tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz && \
    rm go${GO_VERSION}.linux-amd64.tar.gz

# 创建 GOPATH
RUN mkdir -p /go/{bin,src,pkg}
ENV GOPATH=/go
ENV PATH=$PATH:/go/bin

# ========================
# 🦀 安装 Rust 工具链 + 设置国内源
# ========================
# 🇨🇳 设置 Rust 国内镜像（中科大源）
ENV RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
ENV RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
ENV CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse+https://rsproxy.cn/crates.io-index/

RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y

# 激活 rust 环境（重要！）
ENV PATH="/root/.cargo/bin:${PATH}"

# ========================
# 🧑‍💻 用户和 Shell 优化
# ========================
# RUN useradd -m -s /bin/zsh dev && \
#    echo "dev:dev" | chpasswd && \
#    usermod -aG sudo dev
RUN echo "dev:dev" | chpasswd && \
    usermod -aG sudo dev && \
    usermod -s /bin/zsh dev

# 安装 oh-my-zsh
RUN git clone https://github.com/ohmyzsh/ohmyzsh.git /root/.oh-my-zsh && \
    cp /root/.oh-my-zsh/templates/zshrc.zsh-template /root/.zshrc && \
    chsh -s /bin/zsh root

RUN git clone https://github.com/ohmyzsh/ohmyzsh.git /home/dev/.oh-my-zsh && \
    cp /home/dev/.oh-my-zsh/templates/zshrc.zsh-template /home/dev/.zshrc && \
    chown -R dev:dev /home/dev/.oh-my-zsh && \
    chsh -s /bin/zsh dev

# 为 dev 用户也设置 pip、npm、cargo 镜像（可选，推荐）
USER dev
WORKDIR /home/dev

# 🇨🇳 为 dev 用户设置 pip 镜像
RUN mkdir -p ~/.config/pip && \
    echo -e "[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple\ntrusted-host = pypi.tuna.tsinghua.edu.cn" > ~/.config/pip/pip.conf

# 🇨🇳 为 dev 用户设置 npm 镜像
RUN npm config set registry https://registry.npmmirror.com

# 🇨🇳 为 dev 用户设置 cargo 镜像（~/.cargo/config.toml）
RUN mkdir -p ~/.cargo && \
    echo -e '[source.crates-io]\nreplace-with = "rsproxy"\n\n[source.rsproxy]\nregistry = "https://rsproxy.cn/crates.io-index"\n\n[net]\ngit-fetch-with-cli = true' > ~/.cargo/config.toml

USER root

# ========================
# 🤖 安装 Claude Code
# ========================
RUN npm install -g @anthropic-ai/claude-code

# ========================
# 🧹 清理缓存和临时文件
# ========================
RUN apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
    npm cache clean --force && \
    pip cache purge

# ========================
# 🚀 启动默认 Shell
# ========================
USER dev
WORKDIR /home/dev
CMD ["/bin/zsh"]
